<?php

namespace App\Api\Three;

use App\Api\ChannelApi;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;

class KaHao extends ChannelApi
{
    protected $appid = 2093;
    protected $appsecret = 'hMPXPWbfGa6GBtWPXD5K9Wnogd1oZW3I';
    protected $url = 'https://su.asense.net/api/';
    public function __construct()
    {
        $this->log = new Logger('KaHao');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/KaHao.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }
    // 下单接口
    public  function orderSubmit($data)
    {
        if (empty($data)) {
            return $this->returnMsg(1, '参数不能为空');
        }
        // return $this->returnMsg(2, '返回空');
        $method = 'postOrder';
        $time = time();
        $data = [
            'app_id' => $this->appid,
            'timestamp' => $time,
            'package_id' => $data['goods_id'],
            'express_address' => $data['post_sheng'] . $data['post_shi'] . $data['post_qu'] . $data['post_address'],
            'mobile' => $data['contact_phone'],
            'contact' => $data['contact_name'],
            'card_id' => $data['pay_identity'],
            'card_name' => $data['pay_name'],
            'business_order_no' => $data['orderno'],
            // 'card_front' => '', //base64 编码的图片 身份证正面
            // 'card_back'  => '', // base64 编码的图片 身份证反面
            // 'card_user' => '', // base64 编码的图片 手持身份证照片
        ];
        // 加密
        $data['sign'] = $this->getSign($data);
        // var_dump($data['sign']);
        $data = json_encode($data);
        $return = $this->send($method, $data);
        if (!empty($return) && is_string($return)) {
            $return = json_decode($return, true);
        }
        if (isset($return['code']) && $return['code'] == 0) {
            return $this->returnMsg(200, '提交成功');
        }
        return $this->returnMsg(201, '提交中', $return['msg']);
    }
    //查询接口
    public function pushOrder($data)
    {
        $status = [0 => '待开卡', 1 => '开卡中', 2 => '开卡成功', 3 => '开卡失败', 4 => '开卡异常', 6 => '待审核', 7 => '已创建'];
        $expressNameStatus = [
            10 => '圆通',
            11 => 'EMS',
            12 => '顺丰',
            13 => '中通',
            14 => '京东',
            16 => '申通',
            17 => '邮政',
            18 => '恒宇运通',
            24 => '韵达'
        ];
        $cuStatus = [
            0 => 0,
            1 => 10,
            2 => 10,
            6 => 10,
            7 => 30,
            4 => 50,
            3 => 60
        ];
        $method = 'getOrderStatus';
        $time = time();
        $data = [
            'business_order_no' => $data['orderno'],
            'app_id' => $this->appid,
            'timestamp' => $time,
            // 'sign'  => ''
        ];
        $data['sign'] = $this->getSign($data);
        $data = json_encode($data);
        $return = $this->send($method, $data);
        if (!empty($return) && is_string($return)) {
            $return = json_decode($return, true);
        }
        if (isset($return['code']) && $return['code'] == 0) {
            $returnMsg = $return['data'];
            // 修改状态
            $update = [
                'status' => $cuStatus[$returnMsg['open_status']],
            ];
            if (!empty($returnMsg['express_name'])) {
                $update['express_company'] = $expressNameStatus[$returnMsg['express_name']];
                $update['express_number'] = $returnMsg['express_no'];
            }
            if (!empty($returnMsg['open_status'])) {
                $update['card_status'] = $returnMsg['open_status'];
                $update['card_status_desc'] = $status[$returnMsg['open_status']];
            }
            if (!empty($returnMsg['open_message'])) {
                $update['card_status_desc'] = $returnMsg['open_message'];
            }
            if (!empty($returnMsg['phonenum'])) {
                $update['iccid'] = $returnMsg['phonenum'];
            }
            $update['sup_orderno'] = $returnMsg['order_no'];
            $this->editOrderStatus($update);
            return $this->returnMsg(200, '提交成功', $return['data']);
        }
        return $this->returnMsg(100, '订单查询失败', $return);
    }
    public function getSign($data)
    {
        ksort($data);
        $str = '';
        if (is_array($data)) {
            $str = http_build_query($data);
        }
        $str  .= $this->appsecret;
        return strtolower(md5($str));
    }
    public function send($method, $param)
    {
        $url = $this->url . $method;
        $timeout = 10000;
        $header = [
            'content-type:application/json'
        ];
        $ch = curl_init();
        if (is_array($param)) {
            $urlparam = http_build_query($param);
        } else if (is_string($param)) { //json字符串
            $urlparam = $param;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //设置超时时间
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1); //POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam); //post数据
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        if (curl_errno($ch)) {
            $error = curl_error($ch);
            return $error;
        }
        curl_close($ch);
        return $data;
    }
}
